//
//  HolderView.swift
//  AnimationGuard
//
//  Created by onemade on 11/4/15.
//  Copyright © 2015 CNPC. All rights reserved.
//

import UIKit
import Foundation

protocol HolderViewDelegate:class{
    func animateLabel()
}

class HolderView: UIView {
    var parentFrame: CGRect = CGRectZero
    weak var delegate:HolderViewDelegate?
    
    var ovalLayer = OvalLayer()
    let triangle = TriangleLayer()
    let redRectangleLayer = RectangleLayer()
    let blueRectangleLayer = RectangleLayer()
    let arcLayer = ArcLayer()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = Colors.clear
    }
    
    required init(coder: NSCoder) {
        super.init(coder: coder)!
    }
    
    func addOval(){
        layer.addSublayer(ovalLayer)
        ovalLayer.expand()
        
        NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: "wobbleOval", userInfo: nil, repeats: false);
    }
    func wobbleOval(){
        layer.addSublayer(triangle)
        ovalLayer.wobble()
        NSTimer.scheduledTimerWithTimeInterval(0.9, target: self, selector: "drawAnimatedTriangle", userInfo: nil, repeats: false)
    }
    func drawAnimatedTriangle(){
        triangle.animate()
        NSTimer.scheduledTimerWithTimeInterval(0.9, target: self, selector: "spinAndTransform", userInfo: nil, repeats: false)
    }
    
    func spinAndTransform(){
        layer.anchorPoint = CGPoint(x: 0.5, y: 0.6)
        
        let rotationAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnimation.toValue = CGFloat(M_PI * 2)
        rotationAnimation.duration = 0.45
        rotationAnimation.removedOnCompletion = true
        layer.addAnimation(rotationAnimation, forKey: nil)
        
        ovalLayer.contract()
        
        NSTimer.scheduledTimerWithTimeInterval(0.45, target: self, selector: "drawRedAnimatedRectangle", userInfo: nil, repeats: false)
        NSTimer.scheduledTimerWithTimeInterval(0.65, target: self, selector: "drawBlueAnimatedRectangle", userInfo: nil, repeats: false)
    }
    
    func drawRedAnimatedRectangle(){
        layer.addSublayer(redRectangleLayer)
        redRectangleLayer.animateStrokeWithColor(Colors.red)
    }
    func drawBlueAnimatedRectangle(){
        layer.addSublayer(blueRectangleLayer)
        blueRectangleLayer.animateStrokeWithColor(Colors.blue)
        
        NSTimer.scheduledTimerWithTimeInterval(0.9, target: self, selector: "drawArc", userInfo: nil, repeats: false)
    }
    
    func drawArc(){
        layer.addSublayer(arcLayer)
        arcLayer.animate()
        
        NSTimer.scheduledTimerWithTimeInterval(0.9, target: self, selector: "expendView", userInfo: nil, repeats: false)
    }
    
    func expendView(){
        backgroundColor = Colors.blue
        frame = CGRect(x: frame.origin.x - blueRectangleLayer.lineWidth, y: frame.origin.y - blueRectangleLayer.lineWidth, width: frame.size.width + blueRectangleLayer.lineWidth * 2, height: frame.size.height + blueRectangleLayer.lineWidth * 2)
        
        layer.sublayers = nil
        
        UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveEaseInOut, animations: {
                self.frame = self.parentFrame
            }, completion: { _ in
                self.addLabel()
        })
    }
    
    func addLabel(){
        delegate?.animateLabel()
    }
}
